#!/usr/bin/python
'''
metis2clusters.py
	input: output file from pmetis or kmetis
	output: clusters file to use with addClusterLabels.py

	Currently assumes node numbering starting at 1.  
	Also, may need to add vertex label map like was done
	for community as gaps in the range of node values causes
	errors.

usage:
	metis2clusters.py input > output
	
'''

import sys

def processInput(f):
	clusters = dict()
	v = 0
	for line in f.readlines():
		v = v + 1
		c = line.split()[0]
		if ( clusters.has_key(c) ):
			clusters[c].append(str(v))
		else:
			clusters[c] = [str(v)]

	return clusters

def genOutput(clusters, f):
	for k in clusters.keys():
		f.write(k + ":")
		for v in clusters[k]:
			f.write(" " + v)
		f.write("\n")

def main():
	if ( len(sys.argv) != 2 ): 
		print("usage:\n  metis2clusters.py input > output\n")
		sys.exit()	

	inputfile = file(sys.argv[1], 'r')
	outputfile = sys.stdout
	

	# process edge list
	clusters = processInput(inputfile)
	inputfile.close()

	# genereate dot file from graph list
	genOutput(clusters, outputfile)


if __name__ == '__main__':
	main()
